﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using System.Data.Objects.SqlClient;

namespace HXDNB.DAL
{
    public class ProductBLL : BaseDAL<Product>
    {
        public ProductBLL() : base("Product") { }

        /// <summary>
        /// 查询产品列表
        /// </summary>
        public IQueryable<ProductList> GetList(int ClassID = -1)
        {
            var list = from i in _e.Product
                       join j in _e.Class on i.ClassID equals j.ID into Temp
                       from t in Temp.DefaultIfEmpty()
                       where ClassID < 0 || i.ClassID == ClassID
                       orderby i.Stock ascending
                       select new ProductList
                       {
                           ID = i.ID,
                           ClassID = i.ClassID,
                           Icon = i.Icon,
                           ProductName = i.ProductName,
                           Remarks = i.Remarks,
                           Stock = i.Stock,
                           Type = i.Type,
                           ClassName = t.Name,
                           Price = i.Price,
                           Weight = i.Weight,
                           PfPrice = i.PfPrice,
                           OutPrice = i.OutPrice,
                           Description = i.Description
                       };
            return list;
        }

        /// <summary>
        /// 查询产品列表
        /// </summary>
        public List<SelectListItem> GetForSelect(SelectListItem first = null)
        {
            var list = (from i in _e.Product
                        join j in _e.Class on i.ClassID equals j.ID into Temp
                        from t in Temp.DefaultIfEmpty()
                        orderby i.ClassID, i.ID ascending
                        select new SelectListItem() { Text = t.Name + ">" + i.ProductName, Value = SqlFunctions.StringConvert((decimal)i.ID) }).ToList();
            if (first != null)
            {
                list.Insert(0, first);
            }
            return list;
        }
        /// <summary>
        /// 查询产品列表
        /// </summary>
        public List<Product> GetList()
        {
            var list = (from i in _e.Product
                        orderby i.ID ascending
                        select i).ToList();
            return list;
        }
        /// <summary>
        /// 更新库存
        /// </summary>
        public bool AddStock(int ProductId, int Num)
        {
            Product product = this.GetByKey(ProductId);
            product.Stock = (product.Stock ?? 0) + Num;
            return this.update(product);
        }
    }
}
